<?php
class Profesional extends AppModel {
	var $name = 'Profesional';
	var $validate = array(
		'nombre' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'No puede ser vacio',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'apellido' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'No puede ser vacio',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'cedula' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				'message' => 'No puede ser vacio',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);
	//The Associations below have been created with all possible keys, those that are not needed can be removed

	var $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

        function saveImage($fileData) {
                $fileData['name'] = $this->getLocationName($fileData['name']);
		$uploadedPath = WWW_ROOT . "img" . DS. "img_profesionales" .DS. $fileData['name'];
                
		if (move_uploaded_file($fileData['tmp_name'], $uploadedPath)) {
			//ok, everything is fine
		} else {
                     return NULL;
		}
		return $fileData['name'];
	}
        
        private function getLocationName($fileName) {
		if(file_exists(WWW_ROOT . "img" . DS . "img_profesionales" . DS . $fileName))
		{
			$found = true;
			for($i = 1; $found == true; $i++)
			{
				$proposedName = $i . $fileName;
				$found = file_exists(WWW_ROOT . "img" . DS . "img_profesionales" . DS . $proposedName);
			}
			return $proposedName;
		}
		else
		{
			return $fileName;
		}
	}

        function delImage($filename){
            unlink(WWW_ROOT . "img".DS."img_profesionales".DS. $filename);
            return true;
	}
}
?>